package com.boat.starter.openfeign.filter;

import org.slf4j.MDC;

import com.boat.framework.base.api.HttpConsts;
import com.boat.framework.common.thread.context.AppContext;
import com.boat.framework.common.thread.context.LogContext;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;

/**
 * 调用者，在header中增加跟踪号
 * 
 * @author 周江
 *
 */
@Slf4j
public class FeginCallInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate template) {
		try {
			// 传递traceId
			String traceId = MDC.get(LogContext.KEY_TRACE_ID);

			if (StrUtil.isNotBlank(traceId)) {
				template.header(HttpConsts.RequestHeader.TRACKID, traceId);
				if (log.isDebugEnabled()) {
					log.debug("已传递的trackId={}", LogContext.getTrackId());
				}
			}
		} catch (Exception ex) {
			log.error("设置跟踪号出错：" + ex.getMessage(), ex);
		}

	}

}
